package gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import multiThread.MonteCarloMultiThread;

import singleThread.MonteCarloSingleThread;


public class MainWindow extends JFrame implements ActionListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public JComboBox callPutFlag = new JComboBox();
	public JComboBox s = new JComboBox();		
	public JComboBox x = new JComboBox();
	public JComboBox t = new JComboBox();
	public JComboBox r = new JComboBox();
	public JComboBox b =new JComboBox();
	public JComboBox v = new JComboBox();
	public JComboBox nSteps = new JComboBox();
	public JComboBox nSimulations = new JComboBox();
	
	
	public JProgressBar pb = new JProgressBar();
	public JLabel args = new JLabel("SET OF ARGUMENTS");
	public JLabel result  = new JLabel("Result:");
	public JLabel time  = new JLabel("Run Time:");
	private JButton startSingleThreadButton = new JButton("Sequential");
	private JButton startMultiThreadButton = new JButton("Concurrential");
	private JButton exitButton = new JButton("Quit");
	
	public MainWindow(ArrayList<String[]> s){
		super();
		build(s);
	}
	
	private void build(ArrayList<String[]> s){
		setTitle("Monte Carlo Simulations");
		setSize(800,400);
		setLocationRelativeTo(null);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//layout initialization
		JPanel pane = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		fillComboBoxes(s);
		
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 6;
		pane.add(args,c);
		
		c.gridwidth = 1;
		c.gridx = 0;
		c.gridy = 1;
		pane.add(new JLabel("Call Put Flag"),c);
		
		c.gridx = 1;
		c.gridy = 1;
		pane.add(callPutFlag, c);
		
		c.gridx = 2;
		c.gridy = 1;
		pane.add(new JLabel("S"), c);
		
		c.gridx = 3;
		c.gridy = 1;
		pane.add(this.s, c);
		
		c.gridx = 4;
		c.gridy = 1;
		pane.add(new JLabel("X"), c);
		
		c.gridx = 5;
		c.gridy = 1;
		pane.add(x, c);
		
		c.gridx = 0;
		c.gridy = 2;
		pane.add(new JLabel("T"), c);
		
		c.gridx = 1;
		c.gridy = 2;
		pane.add(t, c);
		
		c.gridx = 2;
		c.gridy = 2;
		pane.add(new JLabel("r"), c);
		
		c.gridx = 3;
		c.gridy = 2;
		pane.add(r, c);
		
		c.gridx = 4;
		c.gridy = 2;
		pane.add(new JLabel("b"), c);
		
		c.gridx = 5;
		c.gridy = 2;
		pane.add(b, c);
		
		c.gridx = 0;
		c.gridy = 3;
		pane.add(new JLabel("v"), c);
		
		c.gridx = 1;
		c.gridy = 3;
		pane.add(v, c);
		
		c.gridx = 2;
		c.gridy = 3;
		pane.add(new JLabel("nSteps"), c);
		
		c.gridx = 3;
		c.gridy = 3;
		pane.add(nSteps, c);
		
		pb.setStringPainted(true);
		c.gridwidth = 6;
		c.gridx = 0;
		c.gridy = 6;
		pane.add(pb, c);
		
		c.gridx = 0;
		c.gridy = 7;
		pane.add(result, c);
		
		c.gridx = 0;
		c.gridy = 8;
		pane.add(time, c);
		
		exitButton.addActionListener(this);
		c.gridx = 0;
		c.gridy = 9;
		pane.add(exitButton, c);
		
		startSingleThreadButton.addActionListener(this);
		c.gridwidth = 3;
		c.gridx = 0;
		c.gridy = 5;
		pane.add(startSingleThreadButton, c);
		
		startMultiThreadButton.addActionListener(this);
		c.gridx = 3;
		c.gridy = 5;
		pane.add(startMultiThreadButton, c);
		
		this.setContentPane(pane);
	}
	
	private void fillComboBoxes(ArrayList<String[]> s){
		
		for(int i=0; i<s.size(); i++){
				String[] set = s.get(i);
				callPutFlag.addItem(set[0]);
				this.s.addItem(set[1]);
				x.addItem(set[2]);
				t.addItem(set[3]);
				r.addItem(set[4]);
				b.addItem(set[5]);
				v.addItem(set[6]);
				nSteps.addItem(set[7]);
				nSimulations.addItem(set[8]);
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getSource().equals(this.startSingleThreadButton)){
			new Thread(new MonteCarloSingleThread(this)).start();
		}
		
		else if(arg0.getSource().equals(this.startMultiThreadButton)){
			try {
				new MonteCarloMultiThread(this).call();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		else if(arg0.getSource().equals(this.exitButton)){
			System.exit(0);
		}
		
		else{
		}
		
	}

}
